/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MmsDeptExtraInfoController
 * 部门扩展信息表新
 *
 * 修改纪录
 * 2021-06-16 版本：1.0 hudeyong 创建。
 * @version 版本：1.0
 * @author 作者：hudeyong
 * 创建日期：2021-06-16
 */

package com.topscomm.mms.controller;

import java.util.*;

import com.topscomm.cbo.pojo.CboItemDetailsEntity;
import com.topscomm.cbo.pojo.CboItemsEntity;
import com.topscomm.cbo.service.impl.CboItemDetailsService;
import com.topscomm.mms.pojo.MmsPostAuthorizationInfoEntity;
import com.topscomm.mms.service.impl.MmsPostAuthorizationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.mms.controller.auto.MmsDeptExtraInfoControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

import groovyjarjarasm.asm.tree.IntInsnNode;

/**
 * @author: hudeyong
 * @date: 2021-06-16
 * @description:部门扩展信息表新Controller
 * @modify:
 */
@RestController
@RequestMapping("mms/deptExtraInfo")
public class MmsDeptExtraInfoController extends MmsDeptExtraInfoControllerAuto implements IMmsDeptExtraInfoController {
	@Autowired
	private CboItemDetailsService cboItemDetailsService;
	@Autowired
	private MmsPostAuthorizationInfoService mmsPostAuthorizationInfoService;


	/**
	 * @author: hudeyong
	 * @date: 2021-06-16
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_deptcommoncode")) {
			String value = paraMap.get("qry_deptcommoncode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcommoncode = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_deptcommonmark")) {
			String value = paraMap.get("qry_deptcommonmark").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcommonmark = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: hudeyong
	 * @date: 2021-06-16
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: hudeyong
	 * @date: 2021-06-17
	 * @param paraMap
	 * @return
	 * @description:判断选择的部门是否都为一级部门
	 * @modify:
	 */
	@GetMapping(value = "/checkFirstlevelDept")
	public ResponseResult<Object> checkFirstlevelDept(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Map<String, Object> resultMap = new HashMap<>();
			String[] deptcodes = ConvertUtil.convertToString(paraMap.get("deptcode")).split(",");
			String key = "1";
			for(String code : deptcodes) {
				if(code.contains("_")) {
					if(code.length()-code.indexOf("_")-1==4) {
						continue;
					}else {
						key = "0";
						break;
					}
				}else {
					if(code.length()==4) {
						continue;
					}else {
						key = "0";
						break;
					}
				}
			}
			resultMap.put("key", key);
			return ResponseResult.ok(resultMap);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}

	/***
	 * @description: 查询申请人所在部门
	 * @param paraMap
	 * @return
	 * @author: jiangtao1
	 * @date: 2022/4/2 9:09
	 * @modify:
	 */
	@GetMapping(value = "/queryDeptCommoncode")
	public ResponseResult<Object> queryDeptCommoncode(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Map<String, Object> resultMap = new HashMap<>();
			String deptcode = ConvertUtil.convertToString(paraMap.get("deptcode"));
			if(StringUtil.isEmpty(deptcode)) {
				resultMap.put("deptcommoncode", "");
			} else {
				String deptcommoncode = this.service.getDeptCommonCode(deptcode);
				StringBufferProxy sql = new StringBufferProxy();
				sql.appendSingle("code = '{0}' and itemid in (select id from {1} where code = '{2}')"
						,deptcommoncode, CboItemsEntity.tableName,"DeptCommonCode");
				CboItemDetailsEntity detailsEntity = cboItemDetailsService.queryObjectByWhere(sql.toString());
				if(detailsEntity!=null) {
					resultMap.put("deptcommonname", detailsEntity.getName());
				} else {
					resultMap.put("deptcommonname", "");
				}
				resultMap.put("deptcommoncode", deptcommoncode);
			}
			return ResponseResult.ok(resultMap);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}

	/***
	 * @description: 查询申请人所在部门
	 * @param paraMap
	 * @return
	 * @author: jiangtao1
	 * @date: 2022/4/2 9:09
	 * @modify:
	 */
	@GetMapping(value = "/productLineHeader")
	public ResponseResult<Object> productLineHeader(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Map<String, Object> resultMap = new HashMap<>();
			String productlinecode = ConvertUtil.convertToString(paraMap.get("productlinecode"));
			if(StringUtil.isEmpty(productlinecode)) {
				resultMap.put("productlineheader", "");
			} else {
				List<String> productLineList = Arrays.asList(productlinecode.split(","));
				List<Map<String,String>> resMap = new ArrayList<Map<String,String>>();
				for(String code : productLineList) {
					List<MmsPostAuthorizationInfoEntity> postAuthorizationInfoEntities =
							mmsPostAuthorizationInfoService.queryByWhere(" DEPTCOMMONCODE='"+code+"' and POSTCODE in ('2','3') order by POSTWEIGHT desc ");
					if(postAuthorizationInfoEntities.size()>0) {
						Map<String,String> userInfo = new HashMap<>();
						userInfo.put("postcode",code);
						userInfo.put("usercode",postAuthorizationInfoEntities.get(0).getUsercode());
						resMap.add(userInfo);
					}
				}
				resultMap.put("usercode", resMap);
			}
			return ResponseResult.ok(resultMap);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}
}

